package web.admin.action;

import com.opensymphony.xwork2.ActionSupport;
import domain.entities.Compromisopais;
import domain.entities.Mail;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import services.CompromisoPaisService;
import services.CriteriaDrug;
import services.CriteriaMail;
import services.MailService;

import java.util.List;

@Namespace("/Admin/Mails")
public class MailsController extends ActionSupport{
    private CompromisoPaisService paisService;
    private MailService mailService;

    private List<Mail> list;
    private Mail mail;
    private List<Compromisopais> paisList;
    private Integer mailId;

    @Autowired
    public MailsController(CompromisoPaisService paisService, MailService mailService){
        this.paisService = paisService;
        this.mailService = mailService;
        paisList = paisService.findAll();
    }

    @Action(value="main", results={
            @Result(name="success", location="/admin.mails.tiles", type="tiles")
    })
    public String execute() {
        return SUCCESS;
    }

    @Action(value="list", results={
            @Result(name="success", location="/admin.mails.tiles", type="tiles")
    })
    public String list() {
        CriteriaMail crS = new CriteriaMail(mail);
        list = mailService.list(crS);
        return SUCCESS;
    }

    @Action(value="create", results={
            @Result(name="success",location="/admin.mails.create.tiles", type="tiles")
    })
    public String createMail() {
        if(mailId != null){
            mail = mailService.findById(mailId);
        }
        return SUCCESS;
    }

    @Action(value="save", results={
            @Result(name="success",location="main", type="redirectAction")
    })
    public String saveMail() {
        Compromisopais pais = paisService.findById(mail.getPais().getId());
        mail.setPais(pais);
        mailService.save(mail);
        return SUCCESS;
    }

    public List<Mail> getList() {
        return list;
    }

    public void setList(List<Mail> list) {
        this.list = list;
    }

    public Mail getMail() {
        return mail;
    }

    public void setMail(Mail mail) {
        this.mail = mail;
    }

    public List<Compromisopais> getPaisList() {
        return paisList;
    }

    public void setPaisList(List<Compromisopais> paisList) {
        this.paisList = paisList;
    }

    public Integer getMailId() {
        return mailId;
    }

    public void setMailId(Integer mailId) {
        this.mailId = mailId;
    }
}
